package main.controller;

import main.GlobalConfig;
import main.RequestHelper;
import main.TimeHelper;
import main.database.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserCenterController {
    @RequestMapping (value = "/usercenteraccount")
    public String userCenterAccount(ModelMap modelMap, HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        if (account == null || account.isEmpty()) {
            return "/login";
        }
        else {
            String[] infos = AccountHelper.getAccountInfosFromUUID(uuid);

            if (infos != null) {
                modelMap.put("siteName", GlobalConfig.siteName);
                modelMap.put("nickName", account);
                modelMap.put("phone", infos[1]);
                modelMap.put("email", infos[2]);
                modelMap.put("wechat", infos[3]);
                modelMap.put("zhifubao", infos[4]);
                modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
                modelMap.put("uuid", uuid);
                modelMap.put("money", infos[6]);

                String needVerify = "none";
                if (infos[2].trim().equals("") && infos[1].trim().equals("")) {
                    needVerify = "block";
                }
                modelMap.put("needVerify", needVerify);

                return "userCenterAccountPage";
            }
            else {
                return "redirect:/login";
            }
        }
    }

    @RequestMapping (value = "/usercentercollection")
    public String userCenterCollection(ModelMap modelMap, HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "/userCenterCollectionPage";
        if (account == null || account.isEmpty()) {
            resultUrl =  "/login";
        }
        else {
            List<String[]> infoList = CollectionHelper.getUserCollectionInfos(uuid);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("collectionList", infoList);
            modelMap.put("nickName", account);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
        }
        return resultUrl;
    }

    @RequestMapping (value = "/usercenterdiscuss")
    public String userCenterComment(ModelMap modelMap, HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "/userCenterDiscussionPage";
        if (account == null || account.isEmpty()) {
            resultUrl =  "redirect:/login";
        }
        else {
            List<String[]> commentInfoList = DiscussionHelper.getUserDiscussionInfoList(uuid);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("commentList", commentInfoList);
            modelMap.put("nickName", account);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
        }
        return resultUrl;
    }

    @RequestMapping (value = "/usercentermessage")
    public String userCenterMessage(ModelMap modelMap, HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "/userCenterMessagePage";
        if (account == null || account.isEmpty()) {
            resultUrl = "redirect:/login";
        }
        else {
            List<String[]> messageReceiveList = MessageHelper.getReceivedMessageList(uuid);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("messageReceiveList", messageReceiveList);
            modelMap.put("nickName", account);

            List<String[]> messageSentList = MessageHelper.getSentMessageList(uuid);
            modelMap.put("messageSentList", messageSentList);
            modelMap.put("nickName", account);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
        }
        return resultUrl;
    }

    @RequestMapping (value = "/usercenternotify")
    public String userCenterNotify(ModelMap modelMap, HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "/userCenterNotifyPage";
        if (account == null || account.isEmpty()) {
            resultUrl = "redirect:/login";
        }
        else {
            List<String[]> notifyList = NotifyHelper.getNotifyList(uuid);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("notifyList", notifyList);
            modelMap.put("nickName", account);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
        }
        return resultUrl;
    }

    @RequestMapping(value = "/usercenterlistbookcomment")
    public String userCenterListBookComment(ModelMap modelMap, HttpServletRequest request)
    {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "userCenterListBookCommentPage";
        if (account == null || account.isEmpty()) {
            resultUrl = "/login";
        }
        else {
            List<String[]> infoList = BookCommentHelper.getInfosFromUser(uuid);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
            modelMap.put("nickName", account);
            modelMap.put("bookcommentlist", infoList);
        }

        return resultUrl;
    }

    @RequestMapping (value = "/usercenterupdatebookcomment")
    public String userCenterUpdateBookComment(ModelMap modelMap, HttpServletRequest request, @RequestParam("id") String id) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "userCenterUpdateBookCommentPage";
        if (account == null || account.isEmpty()) {
            resultUrl = "/login";
        }
        else {
            String[] contents = BookCommentHelper.getContents(id);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
            modelMap.put("nickName", account);
            modelMap.put("content", contents[1]);
            modelMap.put("title", contents[0]);
            modelMap.put("id", id);
        }

        return resultUrl;
    }

    @RequestMapping (value = "/usercentercontinuebookcomment")
    public String userCenterContinueBookComment(ModelMap modelMap, HttpServletRequest request, @RequestParam("id") String id) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String resultUrl = "ucContinueBookCommentPage";
        if (account == null || account.isEmpty()) {
            resultUrl = "/login";
        }
        else {
            String[] contents = BookCommentHelper.getContents(id);
            modelMap.put("siteName", GlobalConfig.siteName);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
            modelMap.put("nickName", account);
            modelMap.put("content", contents[1]);
            modelMap.put("title", contents[0]);
            modelMap.put("id", id);
        }

        return resultUrl;
    }
}
